
package com.hc.util;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class JPushUtil {
	
	public static boolean  IS_ONLINE = false;

	public static boolean push(String alis, String message) {
		JPushClient jpushClient = new JPushClient("ff9f050eab54c8674c13ef26", "46aa42817d454275b6a4c2fb", null,ClientConfig.getInstance());
		boolean flag = true;
		try {
			PushResult result = jpushClient.sendPush(buildPushObject_android_ios_alias_alert(alis, message));
			flag = result.isResultOK();
		} catch (APIConnectionException e) {
			buildPushObject_android_ios_alias_alert(alis,message);
		} catch (APIRequestException e) {
			LoggerUtil.error(e);
			flag = false;
		}
		return flag;
	}
	
	/**
	 * 生成极光推送对象PushPayload（采用java SDK）
	 * 
	 * @param alias
	 * @param alert
	 * @return PushPayload
	 */
	public static PushPayload buildPushObject_android_ios_alias_alert(String alias, String alert) {
		return PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.alias(alias))
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(
								AndroidNotification.newBuilder().addExtra("type", "infomation").setAlert(alert).build())
						.addPlatformNotification(
								IosNotification.newBuilder().setSound("sound").addExtra("type", "infomation").setAlert(alert).build())
						.build())
				.setOptions(Options.newBuilder().setApnsProduction(IS_ONLINE)// true-推送生产环境
																			// false-推送开发环境（测试使用参数）
						.setTimeToLive(90)// 消息在JPush服务器的失效时间（测试使用参数）
						.build())
				.build();
	}

/*	public static PushResult push(String alias, String alert) {
		ClientConfig clientConfig = ClientConfig.getInstance();
		JPushClient jpushClient = new JPushClient("f9016aa1cbccf8424b2986d8", "561c8631e768d1c8f3cc9b0f", null,
				clientConfig);
		PushPayload payload = buildPushObject_android_ios_alias_alert(alias, alert);
		try {
			return jpushClient.sendPush(payload);
		} catch (APIConnectionException e) {
			return null;
		} catch (APIRequestException e) {

			return null;
		}
	}*/
	
}
